package com.lovo.sqlatm;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JdbcUtil {

    public static Connection getConnection() {
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //创建连接
        Connection con = null;
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/atm?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "peng18383058225");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("con:" + con);
        return con;
    }

    //添加
    public  void insert(String a_acconut, String a_password, int a_balance) {
        // 创建连接
        Connection con = getConnection();
        PreparedStatement sta = null;
        // 获取sql语句对象
        String str = "insert INTO t_atm(a_account,a_password,a_balance) VALUES(?,?,?)";
        try {
            sta = con.prepareStatement(str);
            sta.setObject(1, a_acconut);
            sta.setObject(2, a_password);
            sta.setObject(3, a_balance);
            //执行SQL语句
            int row = sta.executeUpdate();
            System.out.println("影响行数" + row + "行");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            close(con, sta);
        }
    }

    //通过uesr修改用户信息
    public void update(Uesr uesr) {
        // 创建连接
        Connection con = getConnection();
        PreparedStatement sta = null;
        // 获取sql语句对象
        String str = "update t_atm set a_password=?,a_balance=?  WHERE a_id=?";

        try {
            sta = con.prepareStatement(str);
            sta.setObject(1, uesr.getPassword());
            sta.setObject(2, uesr.getBalance());
            sta.setObject(3, uesr.getId());



            //执行SQL语句
            int row = sta.executeUpdate();
            System.out.println("影响行数" + row + "行");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            close(con, sta);
        }

    }

    //转账的update修改

    public void update(Uesr loginUser,Uesr otherUser) {
        // 创建连接
        Connection con = getConnection();
        PreparedStatement sta = null;
        // 获取sql语句对象
        String str = "update t_atm set a_password=?,a_balance=?  WHERE a_id=?";

        try {
            con.setAutoCommit(false);
            sta = con.prepareStatement(str);
            sta.setObject(1, loginUser.getPassword());
            sta.setObject(2, loginUser.getBalance());
            sta.setObject(3, loginUser.getId());
            sta.executeUpdate();



            sta =con.prepareStatement(str);
            sta.setObject(1, otherUser.getPassword());
            sta.setObject(2, otherUser.getBalance());
            sta.setObject(3, otherUser.getId());
            sta.executeUpdate();
            con.commit();

            //执行SQL语句
            int row = sta.executeUpdate();
            System.out.println("影响行数" + row + "行");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (Exception e){

        }finally {
            //关闭连接
            close(con, sta);
        }

    }


    public void delete(String s_gender, String s_name) {
        Connection con = getConnection();
        PreparedStatement sta = null;
        String delet = "delete from t_student WHERE  s_gender=? or s_name=?";

        try {
            sta = con.prepareStatement(delet);
            sta.setObject(1,s_gender);
            sta.setObject(2,s_name);

            int row = sta.executeUpdate();
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, sta);
//            if (sta != null){
//                try {
//                    sta.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
//
//            if (con !=null){
//                try {
//                    con.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
        }
    }

    /*
     ** 查询
     */
    public static List select() {
        Connection con = getConnection();
        Statement sta = null;
        ResultSet rs = null;
        List useList = new ArrayList();
        try {

            sta = con.createStatement();
            String str = "select * from t_atm";
            rs = sta.executeQuery(str);
            while (rs.next()) {
                Uesr uesr = new Uesr();
                uesr.setAccount(rs.getString(1));
                uesr.setPassword(rs.getString(2));
                uesr.setBalance(rs.getInt(3));
                useList.add(uesr);
                System.out.println(useList);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (sta != null) {
                try {
                    sta.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return (ArrayList) useList;
    }
    /*
    * 根据账号密码查询用户对象
    * */
    public Uesr selectNameAndPwd(String account,String pwd){
        Connection con=getConnection();
        PreparedStatement sta =null;
        ResultSet rs =null;
        Uesr uesr =null;
        String str = "select * from t_atm where a_account=? and a_password=?";
        try {
            sta=con.prepareStatement(str);
            sta.setObject(1,account);
            sta.setObject(2,pwd);
            rs = sta.executeQuery();
            if(rs.next()){
                uesr=new Uesr();
                uesr.setAccount(rs.getString("a_account"));
                uesr.setPassword(rs.getString("a_password"));
                uesr.setBalance(rs.getInt("a_balance"));
                uesr.setId(rs.getInt("a_id"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con,sta,rs);
        }
        return uesr;
    }



    /*
    * 根据账号查询用户对象
    * */
    public Uesr selectName(String acconut) {
        Connection con = getConnection();
        PreparedStatement sta = null;
        ResultSet rs = null;
        Uesr uesr = null;
        String str = "select * from t_atm where a_account=?";
        try {
            sta = con.prepareStatement(str);
            sta.setObject(1, acconut);

            rs = sta.executeQuery();
            if (rs.next()) {
                uesr = new Uesr();
                uesr.setAccount(rs.getString("a_account"));
                uesr.setPassword(rs.getString("a_password"));
                uesr.setBalance(rs.getInt("a_balance"));
                uesr.setId(rs.getInt("a_id"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, sta, rs);
        }
        return uesr;
    }

    public Uesr selectId(int id) {
        Connection con = getConnection();
        PreparedStatement sta = null;
        ResultSet rs = null;
        Uesr uesr = null;
        String str = "select * from t_atm where a_id=?";
        try {
            sta = con.prepareStatement(str);
            sta.setObject(1, id);

            rs = sta.executeQuery();
            if (rs.next()) {
                uesr = new Uesr();
                uesr.setAccount(rs.getString("a_account"));
                uesr.setPassword(rs.getString("a_password"));
                uesr.setBalance(rs.getInt("a_balance"));
                uesr.setId(rs.getInt("a_id"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,sta,rs);
        }
        return uesr;
    }


    /*
     **封装coles 关闭流
     */
    public void close(Connection con, PreparedStatement statement) {
        if (statement != null) {
            try {
                statement.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


    }

    public  void close(Connection con,PreparedStatement statement,ResultSet resultSet) {
        if (statement != null) {
            try {
                resultSet.close();
                statement.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}